package com.dlnu.shoping.shopping.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dlnu.shoping.shopping.common.Result;
import com.dlnu.shoping.shopping.entity.TestUser;
import com.dlnu.shoping.shopping.mapper.TestUserMapper;
import com.dlnu.shoping.shopping.service.ILoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class ILoginServiceImpl implements ILoginService{
    @Resource
    private TestUserMapper testUserMapper;
    @Override
    public Result login(String username, String password) {
        System.out.println(username);
        System.out.println(password);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",username);

        TestUser user = testUserMapper.selectOne(wrapper);
        if(user==null){
            Result result = Result.FAIL;
            result.setMsg("用户名不存在");
            return result;
        }
        else if(!password.equals(user.getPassword())){
            Result result = Result.FAIL;
            result.setMsg("密码错误");
            return result;
        }else{
            return Result.SUCCESS;
        }

    }

    @Override
    public Result addUser(String username, String password, String nickname) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",username);
        TestUser queryUser = testUserMapper.selectOne(wrapper);
        if(queryUser==null){
            TestUser user = new TestUser();
            user.setUsername(username);
            user.setPassword(password);
            user.setNickname(nickname);
            testUserMapper.insert(user);
            return Result.SUCCESS;
        }else{
            Result result = Result.FAIL;
            result.setMsg("用户已存在");
            return result;
        }

    }

    @Override
    public Result delUser(Integer userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        TestUser queryUser = testUserMapper.selectOne(wrapper);
        if (queryUser==null){
            Result result = Result.FAIL;
            result.setMsg("用户不存在");
            return result;
        }else{
            testUserMapper.deleteById(userId);
            return Result.SUCCESS;
        }

    }

    @Override
    public Result changePassword(Integer userId, String newPassword, String oldPassword) {
        TestUser user = testUserMapper.selectById(userId);
        if(user == null) {
            Result result = Result.FAIL;
            result.setMsg("用户不存在");
            return result;
        }else {
            if(oldPassword.equals(user.getPassword())) {
                user.setPassword(newPassword);
                testUserMapper.updateById(user);
                return Result.SUCCESS;
            }else{
                Result result = Result.FAIL;
                result.setMsg("原始密码错误");
                return result;
            }
        }
    }


    @Override
    public Result queryUserList(String username) {
        QueryWrapper wrapper = new QueryWrapper();

        if(StringUtils.isNotEmpty(username)){
            wrapper.like("username",username);
        }
        List<TestUser> list = testUserMapper.selectList(wrapper);
        return new Result(list);
    }

    @Override
    public Result queryUserList2(String username, String nickname) {
        List list = testUserMapper.queryUserList(username, nickname);
        return new Result(list);
    }

    @Override
    public Result queryUserCount(String username) {
        HashMap map = testUserMapper.queryUserCount();
        return new Result(map);
    }

    @Override
    public Result queryUserPage(String username, Integer pageNo, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty(username)) {
            wrapper.like("username",username);
        }
        Page page = new Page(pageNo,pageSize);
        IPage<TestUser> iPage = testUserMapper.selectPage(page,wrapper);
        return new Result(iPage);
    }

    @Override
    public Result queryUserPage2(String username, String nickname, Integer pageNo, Integer pageSize) {
        Page page = new Page(pageNo,pageSize);
        IPage ipage = testUserMapper.queryUserPage(page,username,nickname);
        return new Result(ipage);
    }


}
